#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>

void ls(struct sockaddr_in addr)
{
	int fd,ret;
	char filename[1024];
	
	/*买电话*/
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) 
	{
		perror("socket");
		exit(1);
	}
	int on = 1;
	setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on)); //重用端口号

	/*打电话*/ 
	ret = connect(fd, (struct sockaddr*)&addr, sizeof(addr));	
	if (ret == -1) 
	{
		perror("connect");
		exit(1);
	}
	
	/*发送命令*/
	ret = write(fd,"ls\n", 3);
	if (ret == -1) 
	{
		perror("write");
		exit(1);
	}

	/*接收从服务端返回的结果*/
	printf("----------------ftp files----------------\n");
	while(read(fd,filename,1024) > 0)
		printf("%s",filename);//自带换行符
	printf("----------------ftp files----------------\n");	

	/*挂电话*/
	close(fd);
}

